<?php
class IndexAction extends CommonAction {
    public function index(){
		$this->assign('home','home');
		$ModulesDB = D('Modules');
		$this->initView();
		echo $this->view -> fetch('Public:header');
		echo $ModulesDB->getListToString($this->siteInfo['page_header_id'],$this->view);
		echo $ModulesDB->getListToString($this->siteInfo['page_id'],$this->view);
		echo $ModulesDB->getListToString($this->siteInfo['page_footer_id'],$this->view);
		echo $this->view -> fetch('Public:footer');
		$this->assign('modulesList',$modulesList);
		$this->display();
    }
	
	
	
	
	
	/*delete from think_member where site_id = 75;
delete from think_product_category where site_id = 75;
delete from think_product where site_id = 75;
delete from think_member_member where site_id = 75;*/
	
	
	public function os(){
		$this->initView();
	    echo 	D('Modules') -> getModulesSource(9476,$this->view);
	}
	
	
	
	//update pid 
	public function updatePid(){
		$mdb = D('Member');		
		if(!isset($_GET['pages'])){
			
			
			
			$list = $mdb -> where(array('site_id' => __SITEID__)) -> limit('5') -> order('id') -> select();
			
			
			
		}else{
			
			
			$list = $mdb -> where(array('site_id' => __SITEID__)) -> limit(5*$_GET['pages'].',5') -> order('id') -> select();
			
			
			
			
		}
		
	    if(!$list){
			M('Member') -> where(array('id' => 'pid')) -> data(array('pid' => 0)) ->save();
			//更新完成，进入下一步
			$this->success('PID更新成功，进入下一步，准备检查会员关系结构正确性！',U('checkpid'));
		exit;}
		
		foreach($list as $k=>$v){
			
			
			
			$pid = $mdb -> where(array('user_id' => $v['parent_id'], 'site_id' => __SITEID__)) -> getField('id');
			//dump($mdb -> getLastSql());
			$mdb -> where(array('id' => $v['id'])) -> data(array('pid' => $pid)) -> save();
			if(intval($pid) > 0){
				//$mdb -> member_to_member($v['pid'], $v['id']);
			}
		}
		echo '<script>window.location.href="'.U('Index/updatePid',array('pages' => $_GET['pages']+1)).'";</script>';
		exit;
	}
	
	public function getPageNavArr($pid=0,$arr=array()){
		$re= M('Member')->where(array('id'=>$pid,'site_id'=>__SITEID__))->find();
		
		if(in_array($re['pid'], $arr)){
			M('Member') -> where(array('id' => $re['id'])) -> data(array('pid' => 0)) ->save();
		}
		
		if(!is_array($re)){
			return $arr;
		}else{
			$arr[]=$re['id'];
			return $this->getPageNavArr($re['pid'],$arr);
		}
	}
	
	public function checkpid(){
	
		$mdb = D('Member');		
		if(!isset($_GET['pages'])){
			
			
			
			$list = $mdb -> where(array('site_id' => __SITEID__)) -> limit('5') -> order('id') -> select();
			
			
			
		}else{
			
			
			$list = $mdb -> where(array('site_id' => __SITEID__)) -> limit(5*$_GET['pages'].',5') -> order('id') -> select();
			
			
			
			
		}
		
	    if(!$list){
			
			//更新完成，进入下一步
			$this->success('PID更新成功，进入下一步，恢复会员关系数量！',U('addMember_member'));
		exit;}
		
		foreach($list as $k=>$v){
			
			$this->getPageNavArr($v['pid']);
		}
		echo '<script>window.location.href="'.U('Index/checkpid',array('pages' => $_GET['pages']+1)).'";</script>';
		exit;
	}
	
	
	public function addMember_member(){
		$mmdb = D('Member_member');
		$mdb = D('Member');			
		if(!isset($_GET['pages'])){
			
			
			
			$list = $mdb -> where(array('site_id' => __SITEID__)) -> limit('5') -> order('id') -> select();
			
			
			
		}else{
			
			
			$list = $mdb -> where(array('site_id' => __SITEID__)) -> limit(5*$_GET['pages'].',5') -> order('id') -> select();
			
			
			
			
		}
		
	    if(!$list){D('Product_category') -> copy1();exit('end');}
		
		foreach($list as $k=>$v){
			//$pid = $mdb -> where(array('user_id' => $v['parent_id'], 'site_id' => __SITEID__)) -> getField('id');
			//dump($mdb -> getLastSql());
			//$mdb -> where(array('id' => $v['id'])) -> data(array('pid' => $pid)) -> save();
			$mmdb -> where(array('smember_id' => $v['id'])) -> delete();
			if($v['pid'] > 0){
				$mdb -> member_to_member($v['pid'], $v['id']);
			}
			}
		echo '<script>window.location.href="'.U('Index/addMember_member',array('pages' => $_GET['pages']+1)).'";</script>';
		exit;
	}
	
	
	
	
	
	
	
	
	public function member_copy(){
		
		
		
		$udb = M();
		
		
		$mdb = D('Member');		
		if(!isset($_GET['pages'])){
			
			
			
			$list = $udb ->table('ecs_users') -> where() -> limit('5') -> order('user_id') -> select();
			
			
			
		}else{
			
			
			$list = $udb ->table('ecs_users') -> where() -> limit(5*$_GET['pages'].',5') -> order('user_id') -> select();
			
			
			
			
		}
		
	    if(!$list){
			
			//更新完成，进入下一步
			$this->success('已经同步数据，进入下一步，准备恢复会员关系！',U('updatePid'));
		exit;}
		
		foreach($list as $k=>$v){
			$data['site_id'] = __SITEID__;
			//$data['parent_id'] = $pid;
			
			if($v['wxid'] == ''){
				continue;
			}
			$wret = $udb -> table('wxch_user') -> where(array('wxid' => $v['wxid'])) -> find();
			$data['openid'] = $wret['wxid'];
			$data['nickname'] = $wret['nickname'];
			$data['img_url'] = $wret['headimgurl'];
			$data['is_subscribe'] = $wret['subscribe'];
			$data['level'] = $v['isdongjia'];
			$data['user_id'] = $v['user_id'];
			$data['parent_id'] = $v['parent_id'];
			$mdb->data($data)->add();
		}
		echo '<script>window.location.href="'.U('Index/member_copy',array('pages' => $_GET['pages']+1)).'";</script>';
		exit;
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
	
	
	
	public function createThumb(){
		//查找图片
		if($this->_get('id')){
			$map['id'] = array('gt', $this->_get('id'));
		}else{
			$map = array();
		}
		//查找图片
		$db = M('Img');
		$img = $db -> where($map) -> order('id asc') -> find();
		echo $db -> getLastSql();
		import("ORG.Util.Image");
		Image::thumbR('./Public'.$img['img_url'],'./Public'.getThumbPath($img['img_url'], true),'',300,300,true);
		
		if(!$img){
			exit('完成！');
		}else{
			echo "<script>location.href = '".U('createThumb', array('id' => $img['id']))."';</script>";
		}
		
	}
}